package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.dao.StudentDao;
import com.wanke.entity.Banji;
import com.wanke.entity.CheckinDormitory;
import com.wanke.entity.Dormitory;
import com.wanke.entity.Student;

/**
 * Home object for domain model class Student.
 * 
 * @see com.wanke.entity.Student
 * @author Hibernate Tools
 */
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {

	private static final Log log = LogFactory.getLog(StudentDaoImpl.class);

	@Resource(name = "sessionFactory")
	SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#persist(com.wanke.entity.Student)
	 */
	@Override
	public void persist(Student transientInstance) {
		log.debug("persisting Student instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#attachDirty(com.wanke.entity.Student)
	 */
	@Override
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#attachClean(com.wanke.entity.Student)
	 */
	@Override
	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#delete(com.wanke.entity.Student)
	 */
	@Override
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#merge(com.wanke.entity.Student)
	 */
	@Override
	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#findById(int)
	 */
	@Override
	public Student findById(int id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) sessionFactory.getCurrentSession().get("com.wanke.entity.Student", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.StudentDao#findByExample(com.wanke.entity.Student)
	 */
	@Override
	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.Student")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List<Student> findByDormitory(Dormitory dormitory) {
		List<Student> members = new ArrayList<>();
		String hql = "from Student as student where student.dormitory=:dormitory";
		try {
			members = sessionFactory.getCurrentSession().createQuery(hql).setParameter("dormitory", dormitory).list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return members;
	}

	@Override
	public boolean updateStudent(Student student) {
		boolean result=false;
		try {
			sessionFactory.getCurrentSession().update(student);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Student findByBanjiAndName(String name,Banji banji) {
		Student student=new Student();
		try {
			String hql="from Student as student where student.name=:name and student.banji=:banji";
			student=(Student) sessionFactory.getCurrentSession().createQuery(hql).list().get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return student;
	}

//	@Override
//	public boolean updateStudent(int student_id,CheckinDormitory checkinDormitory) {
//		boolean result = false;
//		try {
//			Student student=findById(student_id);
//			List<CheckinDormitory> checkinDormitories=new ArrayList<>();
//			checkinDormitories.add(checkinDormitory);
//			student.setCheckinDormitories(checkinDormitories);
//			 sessionFactory.getCurrentSession().update(student);
////			String hql = "update Student  student set student.checkinDormitories =:checkinDormitories where student.id=:sid";
////			System.out.println(getClass().toString()+"-->"+student.getId());
////			sessionFactory.getCurrentSession().createQuery(hql)
////					.setParameter("checkinDormitories", student.getCheckinDormitories())
////					.setParameter("sid", student.getId()).executeUpdate();
//			result = true;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return result;
//	}


      //yzj
	  public Student findByUserPass(String username, String password) {
		// TODO Auto-generated method stub
		String hql="from Student stu where stu.id=? and stu.password=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0,username);
		query.setString(1,password);
		if(query.list().size()>0)
			return (Student)query.list().get(0);
		return null;
	}
	
	public List<Student> findAllStu(){
		String hql="from Student";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	public void addStu(Student stu){
		try{
			sessionFactory.getCurrentSession().save(stu);
			System.out.println("插入成功");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
